package com.witmore.skyline.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.witmore.skyline.db.entity.CameraDO;
import com.witmore.skyline.db.entity.CameraPicDO;
import com.witmore.skyline.db.entity.CameraTaskDO;
import com.witmore.skyline.db.mapper.CameraMapper;
import com.witmore.skyline.db.mapper.CameraPicMapper;
import com.witmore.skyline.web.controller.enums.CameraPicSourceTypeEnum;
import com.witmore.skyline.web.controller.enums.WitmoreErrorCode;
import com.witmore.skyline.web.controller.vo.result.AccountSession;
import com.witmore.skyline.web.controller.vo.param.CameraListParamVO;
import com.witmore.skyline.web.controller.vo.result.ResponsePageResult;
import com.witmore.skyline.web.controller.vo.result.ResponseResult;
import com.witmore.skyline.web.service.CameraCaptureService;
import com.witmore.skyline.web.service.CameraCaptureTpService;
import com.witmore.skyline.web.service.CameraCaptureYinShiService;
import com.witmore.skyline.web.service.CameraService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Description: CameraCaptureServiceImpl
 * Author: 豆子高
 * Date: 2025/3/15 17:30
 */
@Slf4j
@Service
public class CameraCaptureServiceImpl implements CameraCaptureService {

    @Autowired
    private CameraMapper cameraMapper;
    @Autowired
    private CameraCaptureTpService cameraCaptureTpService;
    @Autowired
    private CameraCaptureYinShiService cameraCaptureYinShiService;
    @Autowired
    private CameraPicMapper cameraPicMapper;
    @Autowired
    private CameraService cameraService;

    @Override
    public ResponseResult<Void> cameraCaptureTask(AccountSession paramVO, String cameraSn) {
        if (StringUtils.isEmpty(cameraSn)) {
            return ResponseResult.fail(WitmoreErrorCode.PARAM_ERROR);
        }
        // 查询摄像头信息
        CameraDO cameraDO = cameraMapper.selectOne(new LambdaQueryWrapper<CameraDO>()
                .eq(CameraDO::getCameraSn, cameraSn)
                .eq(CameraDO::getOrgId, paramVO.getOrgId())
                .eq(CameraDO::getIsDeleted, 0));
        if (cameraDO == null) {
            return ResponseResult.fail(WitmoreErrorCode.CAMERA_NOT_EXIST);
        }
        if (cameraDO.getCameraStatus() == 0) {
            return ResponseResult.fail(WitmoreErrorCode.CAMERA_OFF_LINE);
        }
        // TODO 目前仅支持 萤石摄像头和 TP-Link摄像头
        if (cameraDO.getType() != 1 && cameraDO.getType() != 2) {
            return ResponseResult.fail(WitmoreErrorCode.CAMERA_TYPE_ERROR);
        }
        CameraTaskDO cameraTaskDO = CameraTaskDO.builder()
                .cameraSn(cameraSn)
                .orgId(paramVO.getOrgId())
                .type(cameraDO.getType())
                .manuSource(cameraDO.getType())
                .state(0)
                .accountId(paramVO.getId())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        // TP-Link 摄像头
        if (cameraDO.getType() == 2) {
            String taskId = cameraCaptureTpService.tpCameraCapturePicHandler(cameraDO, cameraTaskDO);
            if (StringUtils.isEmpty(taskId)) {
                return ResponseResult.fail(WitmoreErrorCode.CAMERA_CAPTURE_TASK_FAIL);
            }
        }
        // 萤石摄像头
        if (cameraDO.getType() == 1) {
            boolean isSuccess = cameraCaptureYinShiService.ysCameraCapturePicHandler(cameraDO, cameraTaskDO);
            if (!isSuccess) {
                return ResponseResult.fail(WitmoreErrorCode.CAMERA_CAPTURE_TASK_FAIL);
            }
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<Void> saveCameraUrl(Integer orgId, String cameraSn, String ossUrl) {
        if (StringUtils.isEmpty(cameraSn) || StringUtils.isEmpty(ossUrl) || orgId == null) {
            return ResponseResult.fail(WitmoreErrorCode.PARAM_ERROR);
        }
        CameraDO cameraDO = cameraMapper.selectOne(new LambdaQueryWrapper<CameraDO>()
                .eq(CameraDO::getCameraSn, cameraSn)
                .eq(CameraDO::getOrgId, orgId)
        );
        if (cameraDO == null) {
            return ResponseResult.fail(WitmoreErrorCode.CAMERA_NOT_EXIST);
        }
        List<CameraPicDO> cameraPicDOS = cameraPicMapper.selectList(new LambdaQueryWrapper<CameraPicDO>()
                .eq(CameraPicDO::getCameraSn, cameraSn)
                .eq(CameraPicDO::getOrgId, orgId)
                .eq(CameraPicDO::getPicUrl, ossUrl)
        );
        if (CollectionUtils.isEmpty(cameraPicDOS)) {
            cameraService.saveCameraPic(cameraDO, ossUrl, CameraPicSourceTypeEnum.USER_CAPTURE_IMAGE_LOCAL);
        }
        return ResponseResult.success();
    }

    @Override
    public ResponsePageResult<CameraPicDO> getCameraPicList(CameraListParamVO paramPageVO) {
        if (null == paramPageVO || null == paramPageVO.getPage()
                || null == paramPageVO.getSize()
                || null == paramPageVO.getOrgId()
                || StringUtils.isEmpty(paramPageVO.getCameraSn())) {
            return ResponsePageResult.fail(WitmoreErrorCode.PARAM_ERROR);
        }
        Page<CameraPicDO> cameraPicDOPage = cameraPicMapper.selectPage(new Page<>(paramPageVO.getPage(), paramPageVO.getSize()),
                new LambdaQueryWrapper<CameraPicDO>()
                        .eq(CameraPicDO::getOrgId, paramPageVO.getOrgId())
                        .eq(CameraPicDO::getCameraSn, paramPageVO.getCameraSn())
                        .orderByDesc(CameraPicDO::getCreateTime)
        );
        if (CollectionUtils.isEmpty(cameraPicDOPage.getRecords())) {
            return ResponsePageResult.success(new Page<>());
        }
        return ResponsePageResult.success(cameraPicDOPage);
    }
}
